package com.sunshine.lnuplus.view

import android.content.Context
import android.os.Bundle
import android.view.*
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.sunshine.lnuplus.R

class WeatherDialog : DialogFragment(){

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.dialog_weather, container, false)
        initView(view)
        val params = dialog?.window?.attributes
        params?.apply {
            gravity = Gravity.TOP
            windowAnimations = R.style.top_animation
        }
        dialog?.window?.setBackgroundDrawableResource(R.drawable.dialog_weather_corner)
        return view
    }

    private fun initView(view: View?) {
        val weaSp = context?.getSharedPreferences("weather", Context.MODE_PRIVATE)
        view?.findViewById<TextView>(R.id.wea_location)?.text = weaSp?.getString("city", "城市")
        view?.findViewById<TextView>(R.id.wea_update)?.text = "更新时间：${weaSp?.getString("update_time", "10:08")}"
        view?.findViewById<TextView>(R.id.wea_tem)?.text = "${weaSp?.getString("tem", "18")}°"
        view?.findViewById<TextView>(R.id.wea_wea)?.text = weaSp?.getString("weather", "晴")
        view?.findViewById<TextView>(R.id.wea_tem1)?.text = "${weaSp?.getString("tem2", "-8")}°~${weaSp?.getString("tem1", "8")}°"
        view?.findViewById<TextView>(R.id.wea_win)?.text = "${weaSp?.getString("win", "东北风")} ${weaSp?.getString("win_speed", "2")}"
        view?.findViewById<TextView>(R.id.wea_humidity)?.text = "湿度 ${weaSp?.getString("humidity", "50")}"
        view?.findViewById<TextView>(R.id.wea_air)?.text = "空气质量 ${weaSp?.getString("air", "优")}"
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(activity!!.windowManager.defaultDisplay.width,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
    }
}